package com.ease.model.criterion;

import java.util.List;

import com.ease.common.vo.Entry;

/**
 * 
 * @author nathanleewei
 * 
 */
public class SimpleExpression extends CriterionSupport implements Criterion {
	/**
	 *
	 */
	private static final long serialVersionUID = -8585818510435782380L;
	private String propertyName;
	private Object value;
	private boolean ignoreCase;
	private String op;

	public String getPropertyName() {
		return propertyName;
	}

	public void setPropertyName(String propertyName) {
		this.propertyName = propertyName;
	}

	public Object getValue() {
		return value;
	}

	public void setValue(Object value) {
		this.value = value;
	}

	public boolean isIgnoreCase() {
		return ignoreCase;
	}

	public void setIgnoreCase(boolean ignoreCase) {
		this.ignoreCase = ignoreCase;
	}

	public void setOp(String op) {
		this.op = op;
	}

	public SimpleExpression() {
	}

	protected SimpleExpression(String propertyName, Object value, String op) {
		this.propertyName = propertyName;
		this.value = value;
		this.op = op;
	}

	protected SimpleExpression(String propertyName, Object value, String op, boolean ignoreCase) {
		this.propertyName = propertyName;
		this.value = value;
		this.ignoreCase = ignoreCase;
		this.op = op;
	}

	public SimpleExpression ignoreCase() {
		ignoreCase = true;
		return this;
	}

	public String toString() {
		return propertyName + getOp() + value;
	}

	protected final String getOp() {
		return op;
	}

	public String toSqlString(QueryCriteria queryCriteria) {
		String columnName = queryCriteria.getColumnNameByPropertyName(propertyName, value);
		StringBuffer fragment = new StringBuffer();
		if (ignoreCase) {
			fragment.append(" lower").append('(');
		}
		fragment.append(columnName);
		if (ignoreCase)
			fragment.append(')');
		fragment.append(' ').append(getOp()).append(" ?");

		return fragment.toString();
	}

	public List<Entry<String, Object>> getNamedValue(QueryCriteria queryCriteria) {
		List<Entry<String, Object>> namedValue = this.safeNamedValue(null);
		if (ignoreCase && null != value && value instanceof String)
			namedValue.add(new Entry<String, Object>(propertyName, value.toString().toLowerCase()));
		else
			namedValue.add(new Entry<String, Object>(propertyName, value));
		return namedValue;
	}

}
